﻿Imports System
Imports System.ComponentModel
Imports System.Workflow.ComponentModel

Namespace System.Workflow.Activities.Rules
    Public NotInheritable Class AddedConditionAction
        Inherits RuleConditionChangeAction
        ' Methods
        Public Sub New()
        End Sub

        Public Sub New(ByVal addedConditionDefinition As RuleCondition)
            If (addedConditionDefinition Is Nothing) Then
                Throw New ArgumentNullException("addedConditionDefinition")
            End If
            Me._conditionDefinition = addedConditionDefinition
        End Sub

        Protected Overrides Function ApplyTo(ByVal rootActivity As Activity) As Boolean
            If (rootActivity Is Nothing) Then
                Return False
            End If
            Dim definitions As RuleDefinitions = [TryCast](rootActivity.GetValue(RuleDefinitions.RuleDefinitionsProperty),RuleDefinitions)
            If (definitions Is Nothing) Then
                definitions = New RuleDefinitions
                rootActivity.SetValue(RuleDefinitions.RuleDefinitionsProperty, definitions)
            End If
            Dim flag As Boolean = False
            If definitions.Conditions.RuntimeMode Then
                definitions.Conditions.RuntimeMode = False
                flag = True
            End If
            Try 
                definitions.Conditions.Add(Me.ConditionDefinition)
            Finally
                If flag Then
                    definitions.Conditions.RuntimeMode = True
                End If
            End Try
            Return True
        End Function


        ' Properties
        <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
        Public Property ConditionDefinition As RuleCondition
            Get
                Return Me._conditionDefinition
            End Get
            Set(ByVal value As RuleCondition)
                If (value Is Nothing) Then
                    Throw New ArgumentNullException("value")
                End If
                Me._conditionDefinition = value
            End Set
        End Property

        Public Overrides ReadOnly Property ConditionName As String
            Get
                Return Me._conditionDefinition.Name
            End Get
        End Property


        ' Fields
        Private _conditionDefinition As RuleCondition
    End Class
End Namespace

